var express = require('express');
var router = express.Router();
var my_fs = require("../tools/fs")
// import my_fs from "../tools/fs"
var moment = require('moment');

/* GET home page. */
router.get('/', get_index);

async function get_index(req, res, next) {
  console.log('--------- in  ---------');
  console.log('ip:' + req.ip);
  console.log('xhr:' + req.xhr);
  console.log('protocol:' + req.protocol);


  // my_fs.stat("./test.txt").then(ret=>{
  //   console.log(ret);
  // })
  let time = moment().format("YYYY-MM-DD HH:mm:ss")

  let text = "get|" +  req.protocol + "|" + req.ip + "|" + time + "\n"

  let ret = await my_fs.append("./log/access.log", text)

  if (ret) {
    console.log(ret);
    res.render('index');
    console.log('--------- end ---------');
  }else{
    console.log(ret);
    res.render('error');
    console.log('--------- end ---------');
  }

  // my_fs.append("./log/login.log", text).then(ret=>{
  //   console.log(ret);
  //   res.render('index');
  //   console.log('--------- end ---------');
  // })

}

module.exports = router;
